<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="表名称" prop="tableName">
        <el-select style="width: 300px"
          v-model="queryParams.tableName"
          placeholder="请选择表名称"
          clearable
          @change="tableSelect"
        >
          <el-option
            v-for="(item, index) in tableOptions"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="success" plain icon="el-icon-upload" size="mini" @click="handleSyc">同步</el-button>
      </el-form-item>
    </el-form>
      <el-table @row-click="clickRow" ref="table" :data="dbTableList" @selection-change="handleSelectionChange" v-if="shows" :render-header="linefeed">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column  v-for="item in tableHead" :label="item.label" :property="item.property">
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    <!-- 数据同步对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body @close="clo">
      <el-form :inline="true" ref="form" :model="form" :rules="rules" label-width="200px">
        <el-form-item label="待导入数据库表" prop="oldTableName">
          <el-select
            v-model="form.oldTableName"
            placeholder="请选择待导入数据库表"
            @change="oldTableSelect"
          >
            <el-option
              v-for="(item, index) in oldtableOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="源数据库名" prop="databaseName">
          <el-input v-model="form.databaseName" placeholder="请输入源数据库名" style="width: 217px"/>
        </el-form-item>
        <el-row>
          <el-form-item label="源数据库地址" prop="adress">
            <el-input v-model="form.adress" placeholder="请输入源数据库地址" style="width: 217px"/>
          </el-form-item>
          <el-form-item label="源数据库用户名" prop="user">
            <el-input v-model="form.user" placeholder="请输入源数据库用户名" style="width: 217px"/>
          </el-form-item>
          <el-form-item label="源数据库密码" prop="password">
            <el-input type="password" show-password v-model="form.password" placeholder="请输入源数据库密码" style="width: 217px"/>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="起始位置" prop="startSysId">
            <el-input-number v-model="form.startSysId" controls-position="right" :disabled="dis" style="width: 217px;"/>
          </el-form-item>
          <el-form-item label="结束位置" prop="endSysId">
            <el-input-number v-model="form.endSysId" controls-position="right" style="width: 217px;" @change="endPosition"/>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMdlsTable, listMdlsColumn, allList, sycGcbxsFile, sycGcbxsImage, sycXxzyqkFile, sycGcbxxzyqkImage, sycXxzytsFile, sycGcbxxzytsImage, maxSysId } from "@/api/tool/gen";
export default {
  data() {
    return {
      // 遮罩层
      visible: false,
      // 选中数组值
      tables: [],
      // 选中数组
      sysids: [],
      // 总条数
      total: 0,
      // 表头
      tableHead: [],
      // 非多个禁用
      multiple: true,
      isPrint: false,
      // 弹出层标题
      title: "数据同步信息",
      shows: false,
      dis:true,
      // 是否显示弹出层
      open: false,
      // 显示搜索条件
      showSearch: true,
      // 表数据
      dbTableList: [],
      //查询表名称
      tableOptions:[],
      //表名称
      oldtableOptions:[],
      //源字段信息
      columnOptions:[],
      //待导入字段信息
      newColumnOptions:[],
      //待导入数据库表
      newtableOptions: [{
        value: 'mdls_books_basic',
        label: 'mdls_books_basic'
      }, {
        value: 'mdls_file',
        label: 'mdls_file'
      },{
        value: 'mdls_image',
        label: 'mdls_image'
      },{
        value: 'mdls_periodical_basic',
        label: 'mdls_periodical_basic'
      }],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tableName: undefined,
        tableComment: undefined
      },
      //表参数
      tableParams:{
        pageNum: 1,
        pageSize: 100
      },
      //form表单
      form:{},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getTableList();
  },
  methods: {
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.sysids = selection.map(item => item.sysid);
      this.multiple = !selection.length;
    },
    // 查询表数据
    getTableList() {
      listMdlsTable(this.tableParams).then(res => {
        if (res.code === 200) {
          let data = res.rows;
          let treeAry = [];
          for (let i in data) {
            let v = data[i];
            let node = {};
            node.label = v.tableName;
            node.value = v.tableName;
            treeAry.push(node);
          }
          this.tableOptions = treeAry;
          this.oldtableOptions = treeAry;
        }
      });
    },
    //自适应宽度
    linefeed (h,{column,index}) { // column,index都是el-table中自带的
      let number = column.label.length // 表头字数
      let size = 16 // 字体尺寸
      column.minWidth = number*size // 计算宽度
      return h('div',{class:'table-head',style:{width:'100%'}},[column.label])
    },
    //表名称变更
    tableSelect(val){
      this.queryParams.tableName = val;
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 100;
      listMdlsColumn(this.queryParams).then(res => {
        if (res.code === 200) {
          let data = res.rows;
          let treeAry = [];
          for(let i in data){
            let v = data[i];
            let node = {};
            node.label = v.columnComment;
            node.property = v.columnName.toLowerCase();
            treeAry.push(node);
          }
          this.shows=true;
          this.tableHead = treeAry;
        }
      });
      this.getList();
    },
    //源数据表变更获取字段信息
    oldTableSelect(val){
      //根据待导入表名称获取最大sysid
      maxSysId(val).then(res => {
        if (res.code === 200) {
          if(res.data != null){
            this.form.startSysId = res.data;
            this.form.endSysId = res.data + 1;
          }else{
            this.form.startSysId = 0;
            this.form.endSysId = 1;
          }
        }
      });
    },
    //待导入数据表变更获取字段信息
    newTableSelect(val){
      let queryString = {};
      queryString.tableName = val;
      queryString.pageNum = 1;
      queryString.pageSize = 100;
      listMdlsColumn(queryString).then(res => {
        if (res.code === 200) {
          let data = res.rows;
          let treeAry = [];
          for(let i in data){
            let v = data[i];
            let node = {};
            node.label = v.columnName;
            node.value = v.columnName;
            treeAry.push(node);
          }
          this.newColumnOptions = treeAry;
        }
      });
    },
    getList(){
      allList(this.queryParams).then(res => {
        if (res.code === 200) {
          this.dbTableList = res.rows;
          this.total = res.total;
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 同步按钮操作 */
    handleSyc(){
      this.open = true;
    },
    // 判断结束是否小于开始
    endPosition(val){
      console.log(this.form.startSysId)
      if(this.form.startSysId === "" || this.form.startSysId === undefined){
        this.$modal.msgError("请先选择待导入数据库表!");
        this.form.endSysId = "";
      }
      if(val <= this.form.startSysId){
        this.$modal.msgError("结束位置不能小于开始位置!");
        this.form.endSysId = this.form.startSysId + 1;
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //关闭页面清空
    clo() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        oldTableName: null,
        databaseName: null,
        adress: null,
        user: null,
        password: null,
        startSysId: 0,
        endSysId: 0
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      //判断待导入数据库表名称
      let tableName = this.form.oldTableName;
      if(tableName == "old_gcbxs_mdls_literature_file"){//学术文件
        sycGcbxsFile(this.form).then(res => {
          if (res.code === 200) {
            this.$modal.msgSuccess(res.msg);
            this.open = false;
          }
        });
      }else if(tableName == "old_gcbxs_mdls_literature_image"){//学术图片
        sycGcbxsImage(this.form).then(res => {
          if (res.code === 200) {
            this.$modal.msgSuccess(res.msg);
            this.open = false;
          }
        });
      }else if(tableName == "old_gcbxxzyqk_mdls_literature_file"){//信息资源期刊文件
        sycXxzyqkFile(this.form).then(res => {
          if (res.code === 200) {
            this.$modal.msgSuccess(res.msg);
            this.open = false;
          }
        });
      }else if(tableName == "old_gcbxxzyqk_mdls_literature_image"){//信息资源期刊图片
        sycGcbxxzyqkImage(this.form).then(res => {
          if (res.code === 200) {
            this.$modal.msgSuccess(res.msg);
            this.open = false;
          }
        });
      }else if(tableName == "old_gcbxxzyts_mdls_literature_file"){//信息资源图书文件
        sycXxzytsFile(this.form).then(res => {
          if (res.code === 200) {
            this.$modal.msgSuccess(res.msg);
            this.open = false;
          }
        });
      }else if(tableName == "old_gcbxxzyts_mdls_literature_image"){//信息资源图书图片
        sycGcbxxzytsImage(this.form).then(res => {
          if (res.code === 200) {
            this.$modal.msgSuccess(res.msg);
            this.open = false;
          }
        });
      }
    },
  }
};
</script>
